///////////////////////////////////////////////
///      Product Name: WordQuicker          ///
/// Copyright (c)2007, Marstone Studio      ///
///           Author: marstone              ///
///        Web Site: marstone.net           ///
///      Mail/MSN: marstone@163.com         ///
///         Created: 2007.10.02             ///
///      Last Modified: 2007.10.02          ///
///////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using Word = Microsoft.Office.Interop.Word;

namespace Studio.Windows.WordQuicker
{
    /// <summary>
    /// Document interface
    /// </summary>
    public class WQTableClass : WQTable
    {
        /// <summary>
        /// The real Embedded Word Table Object
        /// </summary>
        Word.Table WordTable = null;

        /// <summary>
        /// Do not support default public constructor
        /// </summary>
        internal WQTableClass() {}

        /// <summary>
        /// Construct with given Word Table
        /// </summary>
        public WQTableClass(Word.Table table)
        {
            WordTable = table;
        }

        public int Width
        {
            get { return WordTable.Columns.Count; }
        }

        public int Height
        {
            get { return WordTable.Rows.Count; }
        }

        /// <summary>
        /// Warning :  you must give row first and then calculate the column
        /// if column given first, there would be something wrong
        /// </summary>
        public void FillCell(int Row, int Column, string TextToFill)
        {
            // Validate Parameters
            AssertLocation(Row, Column);

            // Fill the cell
            WordTable.Rows[Row].Cells[Column].Range.Text = TextToFill;
        }

        protected void AssertLocation(int Row, int Column)
        {
            // Row
            if (Row > WordTable.Rows.Count || Row <= 0)
                throw new Exception("Row parameter is out of range in FillCell.");
            // Column
            if (Column > WordTable.Rows[Row].Cells.Count || Column <= 0)
                throw new Exception("Column parameter");
        }

        /// <summary>
        /// Append a row for this table
        /// </summary>
        public void AppendRow()
        {
            Object beforeRow = WordTable.Rows.Last;
            WordTable.Rows.Add(ref beforeRow);
        }

        public string ReadCell(int Row, int Column)
        {
            // Validate params
            AssertLocation(Row, Column);

            return WordTable.Rows[Row].Cells[Column].Range.Text;
        }

        public int CellCountInRow(int Row)
        {
            if (Row <= 0 || Row > WordTable.Rows.Count)
                return 0;
            else
                return WordTable.Rows[Row].Cells.Count;
        }

        public int CellCountInColumn(int Column)
        {
            if (Column <= 0 || Column > WordTable.Columns.Count)
                return 0;
            else
                return WordTable.Columns[Column].Cells.Count;
        }

    }
}
